﻿<!DOCTYPE html>
<html>
<head>
	<%
	include("/static/include/title.html"){}
	include("/static/include/css.html"){}
	include("/static/include/js.html"){}
	%>
</head>
<body>
<div class="layui-fluid">
	<form id="fUpdate" class="layui-form" action="#" onsubmit="return false" method="post">
		<input type="hidden" id="dialogId" name="dialogId" value="${parameter.dialogId}"/>
		<input type="hidden" id="user.id" name="user.id" value="${user.id}"/>

		<div class="layui-card">
			<div class="layui-card-header">基础信息</div>
			<div class="layui-card-body" style="padding: 15px;">
				<!-- 如一行显示多列数据，参照下面的示例 -->
				<!--
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">属性1：</label>
						<div class="layui-input-inline">
							<input type="text" autocomplete="off" class="layui-input" maxlength="25" title="属性1对应控件">
						</div>
					</div>对应控件
					<div class="layui-inline">
						<label class="layui-form-label">属性2：</label>
						<div class="layui-input-inline">
							<input type="text" autocomplete="off" class="layui-input" maxlength="25" title="属性1对应控件">
						</div>
					</div>
				</div>
				-->
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">账户名称<span style="color:#FF5722;">*</span>：</label>
						<div class="layui-input-inline">
							<input type="text" name="user.userName" placeholder="账户名称" value="${@team.bangbang.common.utility.LogicUtility.getQuotedString(user.userName)}" autocomplete="off" class="layui-input" maxlength="25">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">有效标识<span style="color:#FF5722;">*</span>：</label>
						<div class="layui-input-inline">
							<input type="radio" name="user.activeFlag" value="true" title="有效"<% if(user.activeFlag != null && user.activeFlag) {%> checked<%}%>/>
							<input type="radio" name="user.activeFlag" value="false" title="禁用"<% if(user.activeFlag == null || !user.activeFlag) {%> checked<%}%>/>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">电子信箱<span style="color:#FF5722;">*</span>：</label>
						<div class="layui-input-inline">
							<input type="text" name="user.email" placeholder="电子信箱，登录时使用" value="${@team.bangbang.common.utility.LogicUtility.getQuotedString(user.email)}" autocomplete="off" class="layui-input" maxlength="50">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">登录密码<span style="color:#FF5722;">*</span>：</label>
						<div class="layui-input-inline">
							<input type="text" name="user.password" placeholder="登录密码，留空表示不改变" autocomplete="off" class="layui-input" maxlength="20">
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">所属商户：</label>
					<div class="layui-input-block">
						<input type="text" name="user.partner.partnerName" value="${isEmpty(user.partner)?'':user.partner.partnerName}" autocomplete="off" class="layui-input" readonly onclick="selectPartner();" placeholder="留空表示为运营商">
						<input type="hidden" name="user.partnerId" value="${user.partnerId}"/>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">所在组织：</label>
					<div class="layui-input-block">
						<input type="text" name="user.organization.organizationName" placeholder="选择所在组织" value="${isEmpty(user.organization)?'':user.organization.fullName}" autocomplete="off" class="layui-input" readonly onclick="selectOrganization();">
						<input type="hidden" name="user.organizationId" value="${user.organizationId}"/>
					</div>
				</div>
			</div>
		</div>

		<div class="layui-card">
			<div class="layui-card-header">角色信息</div>
			<div class="layui-card-body" style="padding: 15px;">
				<table width="95%" border="0">
					<tr>
						<td>${roleForUpdate}</td>
					</tr>
				</table>
			</div>
		</div>

		<div class="layui-card">
			<div class="layui-card-header">管理范围</div>
			<div class="layui-card-body" style="padding: 15px;">
			<%if(user.id == 100001) {%>
				<span style="color:#FF5722;">当前用户为保留账户，管理所有范围组织</span>
			<%} else {%>
				<span style="color:#FF5722;">管理范围留空，表示可以管理所有范围组织</span>				
				<button class="layui-btn layui-btn-xs layui-btn-warm" onclick="addOrganization();">
					<i class="layui-icon layui-icon-add-1"></i>
					添加可以管理的组织机构
				</button>
			<%}%>
				<table width="100%" border="0" id="dynamicTable">
              <% if(has(organizationList) && organizationList != null && user.id != 100001){ for(i in organizationList) {%>
                 <tr id="line${iLP.index}">
			        <td>${i.fullName}
				        <input type="hidden" name="parentId" value="${i.parentId}"/>
				        <input type="hidden" name="id" value="${i.id}"/></td>				        
		            <td width="50">
		            	<i class='layui-icon layui-icon-delete' onclick='deleteRow("line${iLP.index}")' style='font-size: 20px; color: #1E9FFF;'></i>
		            </td>
		        </tr>
              <%}}%>
				</table>
			</div>
		</div>

		<div class="layui-form-item layui-layout-admin">
			<div class="layui-input-block">
				<div class="layui-footer" style="left: 0;">
					<button class="layui-btn" onclick="doUpdateAction('user');">修 改</button>
				</div>
			</div>
		</div>
	</form>
</div>
<script>
    layui.use(['form'], function(){
        var form  = layui.form;
        // 渲染表单radio、select、checkbox
        form.render();
    });

    function validate(theForm) {
        // 注意：
        // 如果更改页面表单的控件类型，请在校验判断时做对应修改：
        // 1. 下拉框：使用 $.trim(theForm["控件名称"].value) == ""
        // 2. 单选框/复选框：使用 getChoiceValue("控件名称") == null
        //
        if($.trim(theForm["user.userName"].value) == "") {
            $.message("账户名称不能为空！");
            return false;
        }
        if(getChoiceValue("user.activeFlag") == null) {
            $.message("请选择有效标识！");
            return false;
        }
        if($.trim(theForm["user.email"].value) == "") {
            $.message("电子信箱不能为空！");
            return false;
        }

        return true;
    }

    var orgCount = ${has(organizationList) ? organizationList.~size : 0};
    // 选择所在组织
    function selectOrganization() {
        openDialog("组织机构选择", "../basis/organizationSelect.do?isPublic=true", 1000, 800, function(result) {
   			console.log(result);
   			var arr = result.data;
   			if(arr == null || arr.length == 0) {
                // 清除
                document.forms[0]["user.organization.organizationName"].value = "";
                document.forms[0]["user.organizationId"].value = "";
   				return;
   			}

            var orgs = result.data;
            // 获得组织机构编号和名称
            document.forms[0]["user.organization.organizationName"].value = orgs[0].fullName;
            document.forms[0]["user.organizationId"].value = orgs[0].id;
        });
    }

    // 选择管理的组织机构
    function addOrganization() {
        openDialog("组织机构选择", "../basis/organizationSelect.do?isPublic=true&multiple=true", 1000, 800, function(result) {
   			console.log(result);
   			var arr = result.data;
   			if(arr == null || arr.length == 0) {
                return;
            }

   			for(var i = 0; i < arr.length; i++) {
	            var node = arr[i];
	            // 选择管理的组织机构
	            if(exist(node.id)) {
	                // $.message("所选择的组织机构已经存在于管理范围中！");
	                continue;
	            }
	
	            addRow(node);
   			}
        });
    }

    function exist(orgId) {
        var ids = document.forms[0]["id"];
        if(ids == null) return false;
        if(ids.length == null || ids.length == 0) {
            return (ids.value == orgId);
        }
        for(var i = 0; i < ids.length; i++) {
            if(ids[i].value == orgId) return true;
        }
        return false;
    }

    // 接收数据
    function addRow(node) {
        var parentIdAdd = node.parentId;
        var orgIdAdd = node.id;
        var orgNameAdd = node.fullName;

        if(parentIdAdd == null || parentIdAdd == "null") parentIdAdd = "";

        // 在tblOrg表中显示出来
        var tblOrg = self.tblOrg || document.getElementById("dynamicTable");
        var newRow = tblOrg.insertRow(-1);
        newRow.id = 'line' + (++orgCount);
        td1 = document.createElement("td");
        td2 = document.createElement("td");
        newRow.appendChild(td1);
        newRow.appendChild(td2);

        td1.innerHTML = orgNameAdd + '<input type="hidden" name="parentId" value="' + parentIdAdd + '"><input type="hidden" name="id" value="' + orgIdAdd + '">';
        td2.width = 50;
        td2.innerHTML = '<i class=\'layui-icon layui-icon-delete\' onclick=\'deleteRow(\"' + newRow.id + '\")\' style=\'font-size: 20px; color: #1E9FFF;\'></i>';
    }

    function deleteRow(lineField) {
        var row = document.getElementById(lineField);
        if(row == null) return;

        dynamicTable.deleteRow(row.rowIndex);
    }
    
    // 选择所属商户
    function selectPartner() {
        openDialog("商户选择", "../basis/partnerSelect.do", 1000, 800, function(result) {
   			console.log(result);
   			var arr = result.data;
   			if(arr == null || arr.length == 0) {
                // 清除
                document.forms[0]["user.partner.partnerName"].value = "";
                document.forms[0]["user.partnerId"].value = "";
   				return;
   			}

            var partners = result.data;
            // 获得商户编号和商户名称
            document.forms[0]["user.partner.partnerName"].value = partners[0].partnerName;
            document.forms[0]["user.partnerId"].value = partners[0].id;
        });
    }
</script>
</body>
</html>